//
//  StringTool.m
//  CuponManeger-1
//
//  Created by 준기 반 on 11. 6. 9..
//  Copyright 2011 한성대학교. All rights reserved.
//

#import "StringTool.h"


@implementation StringTool
-(NSString *) FindNum:(NSString *)string{
	int i,x=0,y=0;
	int ccount=0;
	NSString *ptn=@"(?:[0-9a-zA-Z])";
	NSPredicate *apred=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",ptn];
	NSMutableArray* strarr=[[NSMutableArray alloc]initWithCapacity:[string length]];
	NSMutableArray* coupon=[[NSMutableArray alloc]initWithCapacity:[string length]];
	for(i=0;i<[string length];i++){
		NSString *ichar=[NSString stringWithFormat:@"%c",[string characterAtIndex:i]];
		[strarr addObject:ichar];
	}
	coupon=[strarr filteredArrayUsingPredicate:apred];
	for(i=0;i<[string length];i++){
		if([strarr objectAtIndex:i]==[coupon objectAtIndex:ccount]){
			ccount++;
			if(x==0)x=i;
		}
		else{
			if(x>0){
				if(i-x>7&&[strarr objectAtIndex:i-1]==[coupon objectAtIndex:ccount-1]){
					y=i-x;
					i=[string length];
				}
				
				else x=0;
			}
		}
	}
	
	
	////////////////////////////
	//[strarr dealloc];                    
	//[coupon dealloc];				//해야할지 안해도될지 모르겠..
	/////////////////////////
	if(i-x>7){
		return [string substringWithRange:NSMakeRange(x,y)];
		
	}
	else {
		return @"x";
	}
}

-(BOOL)hasDate:(NSString *)string{
	NSRange range;
	range=[string rangeOfString:@"까지"];
	if(range.location==NSNotFound){
		range=[string rangeOfString:@"-"];
		if(range.location==NSNotFound){
			range=[string rangeOfString:@"~"];
			if(range.location==NSNotFound)
				return NO;
		}
	}
	return YES;
	
}

-(NSDate *)FindDate:(NSString *)string{
	
	
	NSRange range;
	BOOL what;
	int i;
	NSString* tempStr=@"";
	int j=0;
	NSDate* Date;
	range=[string rangeOfString:@"까지"];
	what=NO;
	if(range.location==NSNotFound){
		what=YES;
		range=[string rangeOfString:@"-"];
		if(range.location==NSNotFound){
			range=[string rangeOfString:@"~"];
		}
	}
	NSString *ptn=@"(?:[0-9])";
	NSPredicate *apred=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",ptn];
	NSMutableArray* strarr=[[NSMutableArray alloc]initWithCapacity:[string length]];
	NSMutableArray* datearr=[[NSMutableArray alloc]initWithCapacity:[string length]];
	
	
	if(what==NO){
		for(i=range.location-8;i<range.location;i++){
			NSString *ichar=[NSString stringWithFormat:@"%c",[string characterAtIndex:i]];
			[strarr addObject:ichar];
		}
	}
	else if(what==YES){
		for(i=range.location;i<range.location+11;i++){
			NSString *ichar=[NSString stringWithFormat:@"%c",[string characterAtIndex:i]];
			[strarr addObject:ichar];
		}
	}
	
	datearr=[strarr filteredArrayUsingPredicate:apred];
	for(NSString * temp in datearr){
		j++;
	}
	for(i=0;i<j;i++)
		tempStr=[NSString stringWithFormat:@"%@%@",tempStr,[datearr objectAtIndex:i]];
	
	
	
	NSDateComponents *tempdate=[[NSDateComponents alloc]init];
	NSCalendar *tempcal=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
	if(j==8){
		[tempdate setYear:[[tempStr substringToIndex:4]intValue]];
		[tempdate setMonth:[[tempStr substringWithRange:NSMakeRange(4,2)]intValue]];
		[tempdate setDay:[[tempStr substringWithRange:NSMakeRange(6,2)]intValue]+1];
	}
	else if(j==4){
		[tempdate setMonth:[[tempStr substringToIndex:2]intValue]];
		[tempdate setDay:[[tempStr substringWithRange:NSMakeRange(2,2)]intValue]+1];
	}
	
	Date=[tempcal dateFromComponents:tempdate];
	
	//////////////////////////////
	//[strarr dealloc];
	//[datearr dealloc];			//해야할지 안해도될지 모르겠..
	//////////////////////////
	return Date;
}
-(NSString*)TotalTitle:(NSString*)string{
	NSString* num;
	NSArray* temp;
	int count=0;
	num=[self FindNum:string];
	temp=[string componentsSeparatedByString:num];
	for(NSString* temps in temp)
		count++;
	if(count==1)
		return [NSString stringWithFormat:@"%@********",[temp objectAtIndex:0]];
	else{
		return [NSString stringWithFormat:@"%@********%@",[temp objectAtIndex:0],[temp objectAtIndex:1]];
	}
		
}
-(NSString*)FindTitle:(NSString*)string{
	NSRange range;
	NSArray* companyList=[NSArray arrayWithContentsOfFile:[[[NSBundle mainBundle]bundlePath] stringByAppendingPathComponent:@"CompanyList.plist"]];
	for(NSString *companyName in companyList){
		range=[string rangeOfString:companyName];
		if(range.location!=NSNotFound)
			return companyName;
	}
	return @"x";
}
-(BOOL)CheckString:(NSString*)string{
	NSString *title;
	NSString *num;
	NSDate *date;
	BOOL haddate;
	int count=0;

	NSArray *tempstring=[string componentsSeparatedByString:@"\n\n"];
	for(NSString *temp in tempstring)
		count++;
	if(count==1){
		title=[self FindTitle:string];
		num=[self FindNum:string];
		if(title!=@"x"&&num!=@"x")
			return YES;
		else {
			return NO;
		}
	}
	else{
		title=[self FindTitle:[tempstring objectAtIndex:0]];
		num=[self FindNum:[tempstring objectAtIndex:0]];
		haddate = [self hasDate:[tempstring objectAtIndex:0]];
		if(haddate==YES)
			date=[self FindDate:[tempstring objectAtIndex:0]];
		NSString *nowtitle=[self FindTitle:[tempstring objectAtIndex:count-1]];
		NSString *nownum=[self FindNum:[tempstring objectAtIndex:count-1]];					
		BOOL nowhad=[self hasDate:[tempstring objectAtIndex:count-1]];
		NSDate *nowdate;
		if(nowhad==YES)
			nowdate=[self FindDate:[tempstring objectAtIndex:count-1]];
		if([title isEqualToString:nowtitle]==YES&&[nowdate isEqualToDate:date]==YES&&[num isEqualToString:nownum]==NO&&[nownum isEqualToString:@"x"]==NO){
			
			return YES;
		}
		else {
			return NO;
		}
	}
}
-(NSMutableDictionary*)StringToDic:(NSString*)string{
	NSString *title;
	NSString *num;
	NSDate *date;
	NSMutableArray* couponnum=[[NSMutableArray alloc]init];
	BOOL haddate;
	int count=0;
	NSMutableDictionary *dic=[NSMutableDictionary dictionary];
	NSArray *tempstring=[string componentsSeparatedByString:@"\n\n"];
	for(NSString *temp in tempstring)
		count++;
	if(count==1){
		title=[self FindTitle:string];
		num=[self FindNum:string];
		
		haddate=[self hasDate:string];
		if(title!=@"x"&&num!=@"x"){
			[couponnum addObject:num];
			[dic setObject:couponnum forKey:@"CouponNum"];
			[dic setObject:[self TotalTitle:[tempstring objectAtIndex:0]] forKey:@"TotalString"];
			NSLog(@"여기까지옴");

			[dic setValue:title forKey:@"CouponTitle"];
			if(haddate==YES)
				[dic setValue:[self FindDate:string] forKey:@"CouponDate"];
			
			return dic;
		}
	}
	else{
		
		title=[self FindTitle:[tempstring objectAtIndex:0]];
		num=[self FindNum:[tempstring objectAtIndex:0]];
		haddate=[self hasDate:[tempstring objectAtIndex:0]];
		[couponnum addObject:num];
		[dic setValue:title forKey:@"CouponTitle"];
		if(haddate==YES){
			date=[self FindDate:[tempstring objectAtIndex:0]];
			[dic setValue:date forKey:@"CouponDate"];
		}
		for(int i=count;i>1;i--){
			[couponnum addObject:[self FindNum:[tempstring objectAtIndex:i-1]]];
		}
		[dic setValue:couponnum forKey:@"CouponNum"];
		[dic setObject:[self TotalTitle:[tempstring objectAtIndex:0]] forKey:@"TotalString"];
		return dic;
	}
}
			
@end
